<?php
namespace Blog\Controller;

use Org\Util\ArrayToolkit;

class ApiController extends BaseController {
    protected $config = [
        'ctrl_auth' => false,
        'login_auth' => false,
    ];
    public function _initialize()
    {
        parent::_initialize();
        C('LIST_NUMBERS', I('request.limit', 10, 'intval'));
    }
    public function index() {
        $name = I('name');
        $map = [];
        if($name) {
            $cid = D('Category')->getFieldByName($name, 'id');
            $map = ['category_id'=>$cid];
        }
        $model = D('Article');
        $list = $model->where($map)->order('post_date desc, id desc')->get_normal_article();
        foreach ($list['data'] as &$row) {
            $row['cover'] = $row['img'];
            if($row['is_rec']) {
                $row['tags'] = ['推荐'];
            }
        }
        $this->ok($list);
    }
    public function recommend(){
        $name = I('name');
        $map = ['is_rec'=>1];
        if($name) {
            $cid = D('Category')->getFieldByName($name, 'id');
            $map['category_id'] = $cid;
        }
        $model = D('Article');
        $list = $model->where($map)->order('post_date desc, id desc')->get_normal_article();
        foreach ($list['data'] as &$row) {
            $row['cover'] = $row['img'];
            if($row['is_rec']) {
                $row['tags'] = ['推荐'];
            }
        }
        $this->ok($list);
    }
    public function detail() {
        $id = I('id');
        $model = D('Article');
        $info = $model->where($model->normal_map(['id'=>$id]))->find();
        if($info) {
            $info['cover'] = $info['img'];
            if($info['is_rec']) {
                $info['tags'] = ['推荐'];
            }
            if(!session('api_detail_'.$id)) {
                session('api_detail_' . $id, '1');
                $model->where($model->normal_map(['id'=>$id]))->setInc('view');
                ++$info['view'];
            }
            $this->ok($info);
        }
        $this->fail('文章不存在!');
    }
    public function cates() {
        $model = D('Category');
        $cates = $model->getChilds(C('category_code'));
        $cates = ArrayToolkit::index($cates, 'name');
        $this->ok($cates);
    }
    public function about() {
        $cate = D('Category')->field('id')->get_cate('persional')['id'];
        $list = D('Config')->where(['group'=>$cate])->select();
        $conf = [];
        foreach ($list as $row) {
            $conf[$row['name']] = htmlspecialchars_decode($row['value']);
        }
        $this->ok($conf);
    }
    protected function wx_res($msg, $data, $status) {
        if(is_array($msg)) {
            $data = $msg;
            $msg = '';
        }
        $data = [
            'msg'=>$msg,
            'status'=>$status?:1,
            'data'=>$data
        ];
        $this->ajaxReturn($data);
    }
    protected function ok($msg, $data=[]) {
        $this->wx_res($msg, $data, 1);
    }
    protected function fail($msg, $data) {
        $this->wx_res($msg, $data, 0);
    }
}
